package com.yvan.game.merge.controller;

import com.yvan.game.merge.service.UserService;
import com.yvan.game.merge.support.Model;
import com.yvan.game.merge.vo.UserState;
import com.yvan.platform.JsonWapper;
import com.yvan.springmvc.HttpUtils;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.TimeZone;

@RestController
public class RootController {

    @GetMapping("/ok")
    public String ok() {
        return "ok";
    }

    @ApiOperation("获取当前访问属性")
    @GetMapping("/domain")
    public Model<JsonWapper> domain(HttpServletRequest request) {
        JsonWapper jw = new JsonWapper();

        Enumeration<String> enuma = request.getHeaderNames();
        while (enuma.hasMoreElements()) {
            String key = enuma.nextElement();
            jw.set("HeaderNames", key, request.getHeader(key));
        }

        jw.set("ServerName", request.getServerName());
        jw.set("Host", request.getHeader("Host"));
        jw.set("X-Forwarded-For", request.getHeader("X-Forwarded-For"));
        jw.set("RequestURI", request.getRequestURI());
        jw.set("scheme", request.getScheme());
        jw.set("url", request.getScheme() + "://" + request.getHeader("Host") + request.getRequestURI());

        jw.set("currentHost", HttpUtils.currentHost());
        jw.set("currentToken", HttpUtils.currentToken());
        jw.set("currentRemoteIp", HttpUtils.currentRemoteIp());
        jw.set("currentUrl", HttpUtils.currentUrl());

        jw.set("joda.DateTime", new DateTime());
        jw.set("currentTimeMillis", System.currentTimeMillis());
        jw.set("Date", new Date());
        jw.set("Timestamp", new Timestamp(System.currentTimeMillis()));

        Calendar cal = Calendar.getInstance();
        TimeZone timeZone = cal.getTimeZone();
        jw.set("timeZone.getID", timeZone.getID());
        jw.set("timeZone.getDisplayName", timeZone.getDisplayName());

        return Model.newSuccess(jw);
    }
}
